package top.atcra.rpc.transport.config;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import top.atcra.rpc.config.ConfigLoader;
import top.atcra.rpc.transport.common.ClientHeartbeatHandler;
import top.atcra.rpc.transport.common.PendingRequestHandler;
import top.atcra.rpc.transport.request.RPCRequestEncoder;
import top.atcra.rpc.transport.response.RPCResponseDecoder;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ClientRPCChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline()
                .addLast(new LoggingHandler(log.isDebugEnabled() ? LogLevel.DEBUG : LogLevel.WARN))
                .addLast(new IdleStateHandler(0, ConfigLoader.getConfig().getHealthCheckInterval_MS(), 0, TimeUnit.MILLISECONDS))
                .addLast(new ClientHeartbeatHandler())
                .addLast(new RPCRequestEncoder())
                .addLast(new RPCResponseDecoder())
                .addLast(new PendingRequestHandler());
    }
}
